package com.baizhi.user.service.impl;

import com.baizhi.securitymodule.entity.SecurityUser;
import com.baizhi.user.entity.Admin;
import com.baizhi.user.service.IAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IAdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询数据
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        Admin user = adminService.getOne(wrapper);
        //判断
        if(user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        //根据用户查询用户权限列表 toDo 从数据库里面去获取当前用户的权限信息
        List<String> permissionValueList = Arrays.asList("sys:user:add","sys:user:update");
        SecurityUser securityUser = new SecurityUser();
        System.out.println(user);
        securityUser.setUsername(user.getUsername());
        securityUser.setPassword(user.getPassword());
        securityUser.setPermissionValueList(permissionValueList);
        return securityUser;
    }
}
